<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2019/5/1
 * Time: 下午2:27
 */

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;


class GeneratedUnionUid extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generatedUnionUid';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成公会UID';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $startIncrNum = 1000;// 开始递增数
        $endIncrNum = 9999;// 结束递增数

        $generateIndex = Redis::GET("UNION:UID:GENERATED:INDEX");
        if (!empty($generateIndex)) {
            $startIncrNum = $generateIndex + 1;
            $endIncrNum = $startIncrNum + $endIncrNum;
        }

        $generateArray = [];
        for ($i = $startIncrNum; $i < $endIncrNum; $i++) {
            if (filterUUID($i)) {
                continue;
            }
            array_push($generateArray, $i);
        }

        // 设置生成结束递增数
        Redis::SET("UNION:UID:GENERATED:INDEX", $endIncrNum);

        // 数组顺序打乱
        shuffle($generateArray);

        foreach ($generateArray as $value) {
            Redis::RPUSH("UNION:UID:GENERATED:DB", $value);
        }
    }
}